package com.google.code.memoriesalbum.controller.albums;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import com.google.code.memoriesalbum.model.Account;
import com.google.code.memoriesalbum.model.PicasaAlbum;
import com.google.code.memoriesalbum.service.AlbumService;
import com.google.code.memoriesalbum.util.Constants;
import com.google.code.memoriesalbum.util.PicasaUtil;
import com.google.code.memoriesalbum.util.WebHandlerUtil;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.util.ServiceException;

/**
 * アルバム一覧を取得する。
 * @author shin1ogawa
 */
public class IndexController extends Controller {

	@Override
	protected Navigation run() {
		Account account = WebHandlerUtil.getAccount(request, response);
		if (account == null) {
			return null;
		}
		try {
			String id = asString("id");
			if (isGet() && StringUtils.isEmpty(id)) {
				getAlbumList(account);
			} else if (isPost() && StringUtils.isEmpty(id)) {
				createAlbum(account);
			}
		} catch (OAuthException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (ServiceException e) {
			throw new RuntimeException(e);
		}
		return null;
	}

	void createAlbum(Account account) throws OAuthException, MalformedURLException, IOException,
			ServiceException {
		String title = asString("title");
		if (StringUtils.isEmpty(title)) {
			WebHandlerUtil.errorResponse(request, response, HttpServletResponse.SC_BAD_REQUEST,
					"title must not be null.");
			return;
		}
		PicasawebService service = new PicasawebService(Constants.APPLICATION_NAME);
		PicasaUtil.setOAuthCredentials(service, account.getToken(), account.getTokenSecret());
		PicasaAlbum album = new PicasaAlbum();
		album.setAccountKey(account.getKey());
		album.setDate(new Date(System.currentTimeMillis()));
		album.setTitle(title);
		AlbumEntry albumEntry = PicasaUtil.createAlbum(service, account.getEmail(), album);

		PicasaAlbum picasaAlbum = PicasaUtil.toPicasaAlbum(albumEntry);
		Datastore.put(picasaAlbum);
		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.getWriter().println(
				String.format("%s\t%s", picasaAlbum.getKey().getName(), picasaAlbum.getTitle()));
		response.flushBuffer();
	}

	void getAlbumList(Account account) throws OAuthException, IOException, ServiceException {
		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		List<PicasaAlbum> albumList = AlbumService.getAlbumList(account.getKey());
		for (PicasaAlbum picasaAlbum : albumList) {
			PrintWriter writer = response.getWriter();
			writer.println(String.format("%s\t%s", picasaAlbum.getKey().getName(), picasaAlbum
				.getTitle()));
		}
		response.flushBuffer();
	}
}
